// pages/hospital-detail/hospital-detail.js
const app = getApp();

Page({
  data: {
    hospital: {}
  },

  onLoad(options) {
    if (options.id) {
      this.loadHospitalDetail(options.id);
    }
  },

  loadHospitalDetail(id) {
    app.showLoading();
    app.request({
      url: `/hospital/detail/${id}`,
      method: 'GET'
    }).then(res => {
      if (res.success && res.data) {
        const images = res.data.images ? res.data.images.split(',') : [];
        this.setData({
          hospital: {
            ...res.data,
            images: images
          }
        });
      }
    }).catch(err => {
      app.showToast('加载失败');
      console.error(err);
    }).finally(() => {
      app.hideLoading();
    });
  },

  makeAppointment() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      app.showToast('请先登录');
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }, 1500);
      return;
    }
    
    // 跳转到预约页面，传递医院信息
    wx.navigateTo({
      url: `/pages/appointment-create/appointment-create?hospitalId=${this.data.hospital.id}&hospitalName=${this.data.hospital.name}`
    });
  }
});


